//
//  MLZCommunityViewController.m
//  MotherLover
//
//  Created by lanou on 16/1/8.
//  Copyright © 2016年 zj. All rights reserved.
//

#import "MLZCommunityViewController.h"
#import "MLZPostingViewController.h"
#import "BMOBFindRequest.h"
#import "MLZPostsModel.h"
#import "MLZCommentsTableViewCell.h"
#import "DCPicScrollView.h"
@interface MLZCommunityViewController ()<UITableViewDataSource,UITableViewDelegate,MLZPostingViewControllerDelegate>

@property (nonatomic, strong)UITableView *postsTbableView;
@property (nonatomic, strong)NSMutableArray *listArray;
@property (nonatomic, strong)NSString *userName;
@property (nonatomic, strong)NSString *imgUrl;
@end

@implementation MLZCommunityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

   
    
    BmobQuery *que = [BmobQuery queryWithClassName:[NSString stringWithFormat:@"p%@",[BmobUser getCurrentUser].objectId]];
    [que calcInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        if (array.count!=0){
            
            NSDictionary *d = array[0];
            NSLog(@"%@",d);
            _userName = [d objectForKey:@"name"];
            _imgUrl = [d objectForKey:@"imgUrl"];
           
          
        }
    
    }];
    [self addtabbleView];
    [self datapassing];
}
-(void)reloadpost{
    [self datapassing];

}
- (void)datapassing{
    
    
    BmobQuery *bquery = [[BmobQuery alloc]initWithClassName:@"ACM_"];
    bquery.cachePolicy= kBmobCachePolicyNetworkOnly;
    //[bquery clearCachedResult];
    
    [bquery orderByDescending:@"updatedAt"];
    bquery.limit = 100;
   _listArray = [NSMutableArray array];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
   
        
        
        NSArray *arraylist = [NSArray arrayWithArray:array];
       for (BmobObject *object in arraylist) {
         // [object deleteInBackground];
          MLZPostsModel *model = [[MLZPostsModel alloc]initWithObject:object];

            [_listArray addObject:model];
            
        }
            [self.postsTbableView reloadData];
        
           }];


    
}
- (void)addtabbleView{

    _postsTbableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, KW, KH-64-49) style:(UITableViewStylePlain)];
    _postsTbableView.delegate = self;
    _postsTbableView.dataSource = self;
    _postsTbableView.rowHeight= 110;
//    NSArray *array = @[@"lunbo",@"hashiqi"];
//    DCPicScrollView *dcp = [DCPicScrollView  picScrollViewWithFrame:CGRectMake(0, 0, KW, 150) WithImageUrls:array];
//    dcp.AutoScrollDelay = 2.0f;
//    
//    
//    _postsTbableView.tableHeaderView= dcp;
    
    
    
    [self.view addSubview:_postsTbableView];
    
    
    
    
    
    
    




}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{


    return 50;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KW, 50)];
        
        view.backgroundColor=[UIColor whiteColor];

            UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        button.tintColor= [UIColor grayColor];
            button.frame = CGRectMake((KW - 80) / 2 * 2 , 0, (KW - 80) / 3, view.height );
            [button setTitle:@"发帖" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
            [view addSubview:button];
       
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(CLandR, 0, KW -button.width, view.height)];
        lable.text = @"妈妈们都来聊天吧!!萌萌哒!";
        lable.textColor= [UIColor grayColor];
        [view addSubview:lable];
        
        
        
        
        return view;
        
    }
    else
    {
        return NULL;
    }
    
}






///发帖子
- (void)buttonAction:(UIButton *)button
{
    
    if([BmobUser getCurrentUser]){
        
    MLZPostingViewController *post = [[MLZPostingViewController alloc]init];
        post.delegate= self;
       [self presentViewController:post animated:YES completion:nil];
    }else {
    
    
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请登录" delegate: self cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
        
        // 1秒消失
        [self performSelector:@selector(alertViewDismiss:) withObject:alertView afterDelay:3];
    
    
    }
    
    
    
}

//点击进入帖子详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    MLZPostsModel *model = _listArray[indexPath.row];
    if ([_delegate respondsToSelector:@selector(communityJumDetalis:)]) {
        [_delegate communityJumDetalis:model];
    }



}
- (void)alertViewDismiss:(UIAlertView *)alertView
{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return _listArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *idenfire = @"ZJCell";
    MLZCommentsTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:idenfire];
    if(!cell){
    
        cell = [[MLZCommentsTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:idenfire];
        
    
    
    
    }
    MLZPostsModel *model = _listArray[indexPath.row];
    cell.model = model;
    
    

return cell;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
